/**
 * Copyright (c) 2011, Jason Gardner
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.freecake.bonejams;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.util.Log;
import android.widget.Button;
import android.widget.Toast;

import org.freecake.bonejams.database.MusicCollection;

public class MenuView extends Activity {
    
    ProgressDialog pd = null;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu_main);
        ((BoneJamsApplication) this.getApplication()).startMusicCollectionService();
        addButtonHandlers();
    }
    
    private void addButtonHandlers() {
        ((Button) this.findViewById(R.id.btn_album)).setOnClickListener(albumClickListener);
        ((Button) this.findViewById(R.id.btn_allsongs)).setOnClickListener(allsongsClickListener);
        ((Button) this.findViewById(R.id.btn_playlist)).setOnClickListener(playlistClickListener);            
        ((Button) this.findViewById(R.id.btn_artist)).setOnClickListener(artistClickListener);        
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_menu, menu);
        return true;
    }    
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.refresh_library) {
            pd = ProgressDialog.show(this, "Rebuild", "Rebuilding music collection...", true, false);
            new RebuildMusicCollectionTask().execute();
            return true;
        } else {
            return super.onOptionsItemSelected(item);            
        }
    }    
    
    private OnClickListener albumClickListener = new OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(MenuView.this.getApplication(), AlbumListView.class);
            startActivity(intent);
        }
    };

    private OnClickListener artistClickListener = new OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(MenuView.this.getApplication(), ArtistListView.class);
            startActivity(intent);

        }
    };

    private OnClickListener allsongsClickListener = new OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(MenuView.this.getApplication(), SongListView.class);
            startActivity(intent);

        }
    };

    private OnClickListener playlistClickListener = new OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(MenuView.this.getApplication(), PlaylistListView.class);
            startActivity(intent);

        }
    };    
    
    @Override
    protected void onUserLeaveHint() {
        //End the application if the user navigates away (home screen)
        super.onUserLeaveHint();
        finish();
    }       
       
    @Override
    protected void onStop() {
        //End the application if another application takes focus
        super.onStop();
        finish();
    }
        
    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopService(new Intent(MenuView.this, MusicCollection.class));
    }

	private class RebuildMusicCollectionTask extends AsyncTask<Void,Void,Void> {
		protected Void doInBackground(Void... params) {
		    ((BoneJamsApplication) MenuView.this.getApplication()).getMusicCollection().rebuildCollection();
		    return null;
		}
		
		protected void onPostExecute(Void result) {
		    pd.dismiss();
		}
	}    
}
